﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;

namespace Voize
{
    internal partial class fSettings : Form
    {


        public fSettings(CSettings settings)
        {
            InitializeComponent();
            m_Settings = settings;
            m_ExtensionList = new List<string>();
        }


        private CSettings m_Settings;
        private List<string> m_ExtensionList;



        private void bCancel_Click(object sender, EventArgs e)
        {
            if (this.cEnableScrobbling.Checked)
            {
                if (this.tUser.Text != "" && this.tPassword.Text != "")
                {
                    m_Settings.WriteUserInfo(this.tUser.Text, this.tPassword.Text);
                    CScrobblingSupport.Enabled = true;
                }
                else
                {
                    CScrobblingSupport.Enabled = false;
                }
            }
            else
            {
                CScrobblingSupport.Enabled = false;
            }

            this.Close();
        }


        private void fSettings_Load(object sender, EventArgs e)
        {
            if (m_Settings.RememberWindowSizeAndLocation == "true")
                this.CRememberWindowSizeAndLocation.Checked = true;

            if (m_Settings.RememberLastSessionOnStartup == "true")
                this.cRememberLastSession.Checked = true;

            if (m_Settings.MinimizeToSystemTrayOnClose == "true")
                this.cMinToTray.Checked = true;

            if (m_Settings.AskPlaylistSavingOnClose == "true")
                this.cAskPlaylistSaving.Checked = true;


            if (m_Settings.Configuration.GetFileAssociationStatus("mp3"))
                cMp3.Checked = true;
            else
                cMp3.Checked = false;

            if (m_Settings.Configuration.GetFileAssociationStatus("ogg"))
                cOgg.Checked = true;
            else
                cOgg.Checked = false;

            if (m_Settings.Configuration.GetFileAssociationStatus("wav"))
                cWav.Checked = true;
            else
                cWav.Checked = false;

            if (m_Settings.Configuration.GetFileAssociationStatus("mod"))
                cMod.Checked = true;
            else
                cMod.Checked = false;

            if (m_Settings.Configuration.GetFileAssociationStatus("s3m"))
                cS3m.Checked = true;
            else
                cS3m.Checked = false;

            if (m_Settings.Configuration.GetFileAssociationStatus("xm"))
                cXm.Checked = true;
            else
                cXm.Checked = false;

            if (m_Settings.Configuration.GetFileAssociationStatus("m3u"))
                cM3u.Checked = true;
            else
                cM3u.Checked = false;

            if (m_Settings.Configuration.GetFileAssociationStatus("flac"))
                cFlac.Checked = true;
            else
                cFlac.Checked = false;

            if (m_Settings.Password != "")
                tPassword.Text = m_Settings.Password;

            if (m_Settings.User != "")
                tUser.Text = m_Settings.User;

            if (m_Settings.IsScrobblingEnabled == "true")
                cEnableScrobbling.Checked = true;
            else
                cEnableScrobbling.Checked = false;

            if (m_Settings.IsServerEnabled == "true")
                cEnableServer.Checked = true;
        }



        private void fSettings_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.CRememberWindowSizeAndLocation.Checked == true)
                m_Settings.Write("RememberWindowSizeAndLocation", true);
            else
                m_Settings.Write("RememberWindowSizeAndLocation", false);

            if (this.cRememberLastSession.Checked == true)
                m_Settings.Write("RememberLastSessionOnStartup", true);
            else
                m_Settings.Write("RememberLastSessionOnStartup", false);

            if (this.cMinToTray.Checked == true)
                m_Settings.Write("MinimizeToSystemtrayOnClose", true);
            else
                m_Settings.Write("MinimizeToSystemtrayOnClose", false);

            if (this.cAskPlaylistSaving.Checked == true)
                m_Settings.Write("AskPlaylistSavingOnClose", true);
            else
                m_Settings.Write("AskPlaylistSavingOnClose", false);

            m_Settings.Configuration.SaveFileAssociation(m_ExtensionList);

        }



        #region File associations
      


        private void cMp3_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cMp3.Checked == true)
            {
                m_Settings.MakeFileAssociation("mp3");
                m_ExtensionList.Add("mp3");
            }
            else
            {
                m_Settings.RemoveFileAssociation("mp3");
                m_ExtensionList.Remove("mp3");
            }
        }



        private void cOgg_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cOgg.Checked == true)
            {
                m_Settings.MakeFileAssociation("ogg");
                m_ExtensionList.Add("ogg");
            }
            else
            {
                m_Settings.RemoveFileAssociation("ogg");
                m_ExtensionList.Remove("ogg");
            }
        }



        private void cWav_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cWav.Checked == true)
            {
                m_Settings.MakeFileAssociation("wav");
                m_ExtensionList.Add("wav");
            }
            else
            {
                m_Settings.RemoveFileAssociation("wav");
                m_ExtensionList.Remove("wav");
            }
        }



        private void cS3m_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cS3m.Checked == true)
            {
                m_Settings.MakeFileAssociation("s3m");
                m_ExtensionList.Add("s3m");
            }
            else
            {
                m_Settings.RemoveFileAssociation("s3m");
                m_ExtensionList.Remove("s3m");
            }
        }



        private void cMod_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cMod.Checked == true)
            {
                m_Settings.MakeFileAssociation("mod");
                m_ExtensionList.Add("mod");
            }
            else
            {
                m_Settings.RemoveFileAssociation("mod");
                m_ExtensionList.Remove("mod");
            }
        }



        private void cXm_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cXm.Checked == true)
            {
                m_Settings.MakeFileAssociation("xm");
                m_ExtensionList.Add("xm");
            }
            else
            {
                m_Settings.RemoveFileAssociation("xm");
                m_ExtensionList.Remove("xm");
            }
        }



        private void cM3u_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cM3u.Checked == true)
            {
                m_Settings.MakeFileAssociation("m3u");
                m_ExtensionList.Add("m3u");
            }
            else
            {
                m_Settings.RemoveFileAssociation("m3u");
                m_ExtensionList.Remove("m3u");
            }
        }



        private void button1_Click(object sender, EventArgs e) //Select all
        {
            if (this.button1.Text == "Select all")
            {
                this.cM3u.Checked = true;
                this.cMp3.Checked = true;
                this.cOgg.Checked = true;
                this.cWav.Checked = true;
                this.cS3m.Checked = true;
                this.cXm.Checked = true;
                this.cMod.Checked = true;
                this.cFlac.Checked = true;

                this.button1.Text = "Deselect all";
            }
            else
            {
                this.cM3u.Checked = false;
                this.cMp3.Checked = false;
                this.cOgg.Checked = false;
                this.cWav.Checked = false;
                this.cS3m.Checked = false;
                this.cXm.Checked = false;
                this.cMod.Checked = false;
                this.cFlac.Checked = false;

                this.button1.Text = "Select all";
            }
        }

        #endregion

        private void cEnableScrobbling_CheckedChanged(object sender, EventArgs e)
        {
            if (cEnableScrobbling.Checked)
            {
                this.lUser.Enabled = true;
                this.lPassword.Enabled = true;
                this.tUser.Enabled = true;
                this.tPassword.Enabled = true;
                m_Settings.Write("IsScrobblingEnabled", true);
            }
            else
            {
                this.lUser.Enabled = false;
                this.lPassword.Enabled = false;
                this.tUser.Enabled = false;
                this.tPassword.Enabled = false;
                m_Settings.Write("IsScrobblingEnabled", false);
            }
        }



        private void cFlac_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cFlac.Checked == true)
            {
                m_Settings.MakeFileAssociation("flac");
                m_ExtensionList.Add("flac");
            }
            else
            {
                m_Settings.RemoveFileAssociation("flac");
                m_ExtensionList.Remove("flac");
            }
        }



        private void cEnableServer_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cEnableServer.Checked == true)
            {
                int port;
                lPort.Enabled = true;
                nPortNum.Enabled = true;
                bAddUser.Enabled = true;
                port = (int)nPortNum.Value;
                m_Settings.WriteServerInfo(true, port.ToString());
            }
            else
            {
                int port;
                lPort.Enabled = false;
                nPortNum.Enabled = false;
                bAddUser.Enabled = false;
                port = (int)nPortNum.Value;
                m_Settings.WriteServerInfo(false, port.ToString());
            }
        }



        private void nPortNum_ValueChanged(object sender, EventArgs e)
        {
            if (this.cEnableServer.Checked)
            {
                int port;

                port = (int)nPortNum.Value;
                m_Settings.WriteServerInfo(true, port.ToString());
            }
        }



        private void bAddUser_Click(object sender, EventArgs e)
        {
            CRemoteAuthentication autentication = new CRemoteAuthentication();
            fNewUser NewUser = new fNewUser(autentication);
            NewUser.ShowDialog();
        }

    }
}
